package my1024.app;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.KeyboardFocusManager;

import javax.media.j3d.Canvas3D;
import javax.swing.JFrame;
import javax.swing.JPanel;

import my1024.controller.InputProcessing;
import my1024.controller.LogicController;
import my1024.view.RenderingManager;

import com.sun.j3d.utils.universe.SimpleUniverse;

public class MainApp extends JFrame {
	private static final long serialVersionUID = -4441430466121138782L;

	private LogicController logicController = null;
	private RenderingManager renderingManager = null;
	private InputProcessing inputProcessing = null;

	private Canvas3D canvas = null;
	private JPanel drawingPanel = null;

	public MainApp() {
		Const.loadTextureToPool();
		setupDrawingPanel();
		initMVC();
		initKeyListener();
	}

	private void setupDrawingPanel() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("My1024");

		drawingPanel = new JPanel();
		drawingPanel.setLayout(new BorderLayout());
		drawingPanel.setPreferredSize(new Dimension(600, 600));

		getContentPane().add(drawingPanel, BorderLayout.CENTER);
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		this.canvas = new Canvas3D(config);
		drawingPanel.add(canvas, BorderLayout.CENTER);
		pack();
	}

	private void initMVC() {
		logicController = new LogicController();
		renderingManager = new RenderingManager(canvas);
		logicController.addObserver(renderingManager);
	}

	private void initKeyListener() {
		inputProcessing = new InputProcessing(logicController);
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		manager.addKeyEventDispatcher(inputProcessing);
	}

	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new MainApp().setVisible(true);
			}
		});
	}
}
